﻿using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net;
using System.Text;

namespace watchWeb
{
    class GetHtml
    {
        public GetHtml() { }
        public GetHtml(string url)
        {
            m_url = url;
        }
        public void SetUrl(string url)
        {
            m_url = url;
        }
        public string toHtml()
        { 
            HttpWebRequest request;
            HttpWebResponse response;
            string s;
            TextReader tr;
                request = (HttpWebRequest)WebRequest.Create(m_url);
                request.UserAgent = "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)";
                request.ContentType = "application/x-www-form-urlencoded";
                request.Accept = "*/*";
                request.Headers.Add("Accept-Language", "zh-cn,en-us;q=0.5");

                response = (HttpWebResponse)request.GetResponse();
                Stream st = response.GetResponseStream();
                if (response.ContentEncoding.ToLower().Contains("gzip"))
                {
                    st = new GZipStream(st,CompressionMode.Decompress);
                }
                tr = new StreamReader(st,
                    Encoding.GetEncoding(936));//正确解释汉字

                s = tr.ReadToEnd();//保存在字符串中，也可以保存在文件中
                response.Close();
            return s;
        }
        private string m_url;
    }
}
